﻿using System.Collections.Generic;
using System.Linq;

using ORM;
using static Common;
using static DataLogic;

public class List : HandlerBase
{
	/// <summary>
	/// 列出当前用户和目录
	/// </summary>
	/// <returns></returns>
	public dynamic listMeAndDir()
	{
		var me = GetCurrentUser(DbOp);
		var result = new List<dynamic> { new { id = me.Id.ToString(), name = me.Uname_Val, } };
		result.AddRange(DbOp.Dir.GetByFVs().Select(dir => new { id = dir.Id.ToString(), name = dir.Title_Val, dir = true, count = dir.Dir__Photo().Count(), }));
		return result;
	}

	/// <summary>
	/// 列出用户或某个目录下的文件
	/// </summary>
	/// <param name="owner"></param>
	/// <returns></returns>
	public dynamic listPhoto(long owner)
	{
		var me = GetCurrentUser(DbOp);
		var (user, dir) = (DbOp.Users.GetById(owner), DbOp.Dir.GetById(owner));
		return (dir?.Dir__Photo() ?? user.User__Photo()).OrderByDescending(photo => photo.Id).Select(photo =>
		new
		{
			id = photo.Id.ToString(),
			title = photo.Title_Val,
			remark = photo.Remark_Val,
			thumb = $"{photo.Path_Val}?thumb={ThumbQ.List}&update={photo.Updated_Val ?? 0}",
			tip = user == null ? "作者：" + photo.User_Val.Uname_Val : "目录：" + photo.Dir_Val?.Title_Val,
			mine = me == photo.User_Val || me.IsAdmin(),
		});
	}
}